 /*
 * Produtos.java
 *
 * Created on 03/03/2010, 09:08:42
 * Copyright Las
 */
package Soligee;

import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeCadastrarImplements;
import Soligee.decorador.DecoradorInicializaCombos;
import Soligee.decorador.ValidaCamposObrigatorios;
import Soligee.model.vo.cadastros.CategoriaVO;
import Soligee.model.vo.cadastros.MarcaVO;
import Soligee.model.vo.cadastros.ProdutoVO;
import Soligee.model.vo.cadastros.SubcategoriaVO;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.panellayout.PanelLayout;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.paneltabset.PanelTabSet;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import javax.faces.FacesException;
import Soligee.view.ApplicationBean1;
import Soligee.view.RequestBean1;
import Soligee.view.SessionBean1;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;


/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 */
public class Produtos extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private int __placeholder;
    private static int aux =0;
    private static int tamanhoMarca =0;
    private static int tamanhoCategoria =0;
    private static int tamanhoSubCategoria =0;
    ValidaCamposObrigatorios valida = new ValidaCamposObrigatorios();
    protected SessionFacadeBuscarImplements facadeBusca = new SessionFacadeBuscarImplements();

   

    
    SessionFacadeCadastrarImplements cadastrar = new SessionFacadeCadastrarImplements();
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }

    //Combo Marca
    private DefaultSelectedData botaoSelecionadoComboMarca = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoComboMarca() {
        return botaoSelecionadoComboMarca;
    }

    public void setBotaoSelecionadoComboMarca(DefaultSelectedData dsd) {
        this.botaoSelecionadoComboMarca = dsd;
    }
    private DefaultSelectionItems botaoComboMarca = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoComboMarca() {
        return botaoComboMarca;
    }

    public void setBotaoComboMarca(DefaultSelectionItems dsi) {
        this.botaoComboMarca = dsi;
    }

    //ComboCategoria
    private DefaultSelectedData botaoSelecionadoComboCategoria = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoComboCategoria() {
        return botaoSelecionadoComboCategoria;
    }

    public void setBotaoSelecionadoComboCategoria(DefaultSelectedData dsd) {
        this.botaoSelecionadoComboCategoria= dsd;
    }
    private DefaultSelectionItems botaoComboCategoria = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoComboCategoria() {
        return botaoComboCategoria;
    }

    public void setBotaoComboCategoria(DefaultSelectionItems dsi) {
        this.botaoComboCategoria = dsi;
    }

    //Combo SubCategoria
    private DefaultSelectedData botaoSelecionadoComboSubCategoria = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoComboSubCategoria() {
        return botaoSelecionadoComboSubCategoria;
    }

    public void setBotaoSelecionadoComboSubCategoria(DefaultSelectedData dsd) {
        this.botaoSelecionadoComboSubCategoria = dsd;
    }
    private DefaultSelectionItems botaoComboSubCategoria = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoComboSubCategoria() {
        return botaoComboSubCategoria;
    }

    public void setBotaoComboSubCategoria(DefaultSelectionItems dsi) {
        this.botaoComboSubCategoria = dsi;
    }
    private PanelLayout painelAbas = new PanelLayout();

    public PanelLayout getPainelAbas() {
        return painelAbas;
    }

    public void setPainelAbas(PanelLayout pl) {
        this.painelAbas = pl;
    }
    private PanelTabSet painelAbasProduto = new PanelTabSet();

    public PanelTabSet getPainelAbasProduto() {
        return painelAbasProduto;
    }

    public void setPainelAbasProduto(PanelTabSet pts) {
        this.painelAbasProduto = pts;
    }
    private HtmlInputText campoCodProduto = new HtmlInputText();

    public HtmlInputText getCampoCodProduto() {
        return campoCodProduto;
    }

    public void setCampoCodProduto(HtmlInputText hit) {
        this.campoCodProduto = hit;
    }
    private HtmlInputText campoDescricao = new HtmlInputText();

    public HtmlInputText getCampoDescricao() {
        return campoDescricao;
    }

    public void setCampoDescricao(HtmlInputText hit) {
        this.campoDescricao = hit;
    }
    private HtmlSelectOneMenu comboMarca = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboMarca() {
        return comboMarca;
    }

    public void setComboMarca(HtmlSelectOneMenu hsom) {
        this.comboMarca = hsom;
    }
    private HtmlSelectOneMenu comboCategoria = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboCategoria() {
        return comboCategoria;
    }

    public void setComboCategoria(HtmlSelectOneMenu hsom) {
        this.comboCategoria = hsom;
    }
    private HtmlSelectOneMenu comboSubCategoria = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboSubCategoria() {
        return comboSubCategoria;
    }

    public void setComboSubCategoria(HtmlSelectOneMenu hsom) {
        this.comboSubCategoria = hsom;
    }
    private HtmlInputText campoUnidade = new HtmlInputText();

    public HtmlInputText getCampoUnidade() {
        return campoUnidade;
    }

    public void setCampoUnidade(HtmlInputText hit) {
        this.campoUnidade = hit;
    }
    private HtmlInputText campoQuantidadeMinima = new HtmlInputText();

    public HtmlInputText getCampoQuantidadeMinima() {
        return campoQuantidadeMinima;
    }

    public void setCampoQuantidadeMinima(HtmlInputText hit) {
        this.campoQuantidadeMinima = hit;
    }
    private HtmlInputText campoQuantidadeEstoque = new HtmlInputText();

    public HtmlInputText getCampoQuantidadeEstoque() {
        return campoQuantidadeEstoque;
    }

    public void setCampoQuantidadeEstoque(HtmlInputText hit) {
        this.campoQuantidadeEstoque = hit;
    }
    private HtmlInputText campoPrecoCusto = new HtmlInputText();

    public HtmlInputText getCampoPrecoCusto() {
        return campoPrecoCusto;
    }

    public void setCampoPrecoCusto(HtmlInputText hit) {
        this.campoPrecoCusto = hit;
    }
    private HtmlInputText campoPercentualVendas = new HtmlInputText();

    public HtmlInputText getCampoPercentualVendas() {
        return campoPercentualVendas;
    }

    public void setCampoPercentualVendas(HtmlInputText hit) {
        this.campoPercentualVendas = hit;
    }
    private HtmlInputText campoPrecoVenda = new HtmlInputText();

    public HtmlInputText getCampoPrecoVenda() {
        return campoPrecoVenda;
    }

    public void setCampoPrecoVenda(HtmlInputText hit) {
        this.campoPrecoVenda = hit;
    }
    private PopupBean panelPopup1Bean = new PopupBean();

    public PopupBean getPanelPopup1Bean() {
        return panelPopup1Bean;
    }

    public void setPanelPopup1Bean(PopupBean pb) {
        this.panelPopup1Bean = pb;
    }
    private PanelPopup painelPopUpMarca = new PanelPopup();

    public PanelPopup getPainelPopUpMarca() {
        return painelPopUpMarca;
    }

    public void setPainelPopUpMarca(PanelPopup pp) {
        this.painelPopUpMarca = pp;
    }
    private HtmlInputText campoCodigoMarca = new HtmlInputText();

    public HtmlInputText getCampoCodigoMarca() {
        return campoCodigoMarca;
    }

    public void setCampoCodigoMarca(HtmlInputText hit) {
        this.campoCodigoMarca = hit;
    }
    private HtmlInputText campoDescricaoMarca = new HtmlInputText();

    public HtmlInputText getCampoDescricaoMarca() {
        return campoDescricaoMarca;
    }

    public void setCampoDescricaoMarca(HtmlInputText hit) {
        this.campoDescricaoMarca = hit;
    }
    private PopupBean panelPopup1Bean1 = new PopupBean();

    public PopupBean getPanelPopup1Bean1() {
        return panelPopup1Bean1;
    }

    public void setPanelPopup1Bean1(PopupBean pb) {
        this.panelPopup1Bean1 = pb;
    }
    private PanelPopup painelPopUpCategoria = new PanelPopup();

    public PanelPopup getPainelPopUpCategoria() {
        return painelPopUpCategoria;
    }

    public void setPainelPopUpCategoria(PanelPopup pp) {
        this.painelPopUpCategoria = pp;
    }
    private HtmlInputText campoCodigoCatgoria = new HtmlInputText();

    public HtmlInputText getCampoCodigoCatgoria() {
        return campoCodigoCatgoria;
    }

    public void setCampoCodigoCatgoria(HtmlInputText hit) {
        this.campoCodigoCatgoria = hit;
    }
    private HtmlInputText campoDescricaoCatgoria = new HtmlInputText();

    public HtmlInputText getCampoDescricaoCatgoria() {
        return campoDescricaoCatgoria;
    }

    public void setCampoDescricaoCatgoria(HtmlInputText hit) {
        this.campoDescricaoCatgoria = hit;
    }
    private HtmlCommandButton btn_cadastrarCatgoria = new HtmlCommandButton();

    public HtmlCommandButton getBtn_cadastrarCatgoria() {
        return btn_cadastrarCatgoria;
    }

    public void setBtn_cadastrarCatgoria(HtmlCommandButton hcb) {
        this.btn_cadastrarCatgoria = hcb;
    }

    //ComboMarcaPopUpCategoria
    private DefaultSelectedData botaoSelecionadoComboMarcaPopUpCategoria = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoComboMarcaPopUpCategoria() {
        return botaoSelecionadoComboMarcaPopUpCategoria;
    }

    public void setBotaoSelecionadoComboMarcaPopUpCategoria(DefaultSelectedData dsd) {
        this.botaoSelecionadoComboMarcaPopUpCategoria = dsd;
    }
    private DefaultSelectionItems botaoComboMarcaPopUpCategoria = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoComboMarcaPopUpCategoria() {
        return botaoComboMarcaPopUpCategoria;
    }

    public void setBotaoComboMarcaPopUpCategoria(DefaultSelectionItems dsi) {
        this.botaoComboMarcaPopUpCategoria = dsi;
    }
    private PopupBean panelPopup1Bean2 = new PopupBean();

    public PopupBean getPanelPopup1Bean2() {
        return panelPopup1Bean2;
    }

    public void setPanelPopup1Bean2(PopupBean pb) {
        this.panelPopup1Bean2 = pb;
    }
    private PanelPopup painelPopUpSubCategoria = new PanelPopup();

    public PanelPopup getPainelPopUpSubCategoria() {
        return painelPopUpSubCategoria;
    }

    public void setPainelPopUpSubCategoria(PanelPopup pp) {
        this.painelPopUpSubCategoria = pp;
    }

    //ComboMarcaSubCategoria
    private DefaultSelectedData botaoSelecionadoComboMarcaPopUpSubCategoria = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoComboMarcaPopUpSubCategoria() {
        return botaoSelecionadoComboMarcaPopUpSubCategoria;
    }

    public void setBotaoSelecionadoComboMarcaPopUpSubCategoria(DefaultSelectedData dsd) {
        this.botaoSelecionadoComboMarcaPopUpSubCategoria = dsd;
    }
    private DefaultSelectionItems botaoComboMarcaPopUpSubCategoria = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoComboMarcaPopUpSubCategoria() {
        return botaoComboMarcaPopUpSubCategoria;
    }

    public void setBotaoComboMarcaPopUpSubCategoria(DefaultSelectionItems dsi) {
        this.botaoComboMarcaPopUpSubCategoria = dsi;
    }


    //Categoria - ComboCadastrarSubCategoria
     private DefaultSelectedData botaoSelecionadoComboCategoriaPopUpSubCategoria = new DefaultSelectedData();

    public DefaultSelectedData getBotaoSelecionadoComboCategoriaPopUpSubCategoria() {
        return botaoSelecionadoComboCategoriaPopUpSubCategoria;
    }

    public void setBotaoSelecionadoComboCategoriaPopUpSubCategoria(DefaultSelectedData dsd) {
        this.botaoSelecionadoComboCategoriaPopUpSubCategoria = dsd;
    }

    private DefaultSelectionItems botaoComboCategoriaPopUpSubCategoria = new DefaultSelectionItems();

    public DefaultSelectionItems getBotaoComboCategoriaPopUpSubCategoria() {
        return botaoComboCategoriaPopUpSubCategoria;
    }

    public void setBotaoComboCategoriaPopUpSubCategoria(DefaultSelectionItems dsi) {
        this.botaoComboCategoriaPopUpSubCategoria = dsi;
    }
    private HtmlInputText campoCodigoSubCategoria = new HtmlInputText();

    public HtmlInputText getCampoCodigoSubCategoria() {
        return campoCodigoSubCategoria;
    }

    public void setCampoCodigoSubCategoria(HtmlInputText hit) {
        this.campoCodigoSubCategoria = hit;
    }
    private HtmlSelectOneMenu comboMarcaPopUpSubCategoria = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboMarcaPopUpSubCategoria() {
        return comboMarcaPopUpSubCategoria;
    }

    public void setComboMarcaPopUpSubCategoria(HtmlSelectOneMenu hsom) {
        this.comboMarcaPopUpSubCategoria = hsom;
    }
    private HtmlSelectOneMenu comboCategoriaPopUpSubCategoria = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboCategoriaPopUpSubCategoria() {
        return comboCategoriaPopUpSubCategoria;
    }

    public void setComboCategoriaPopUpSubCategoria(HtmlSelectOneMenu hsom) {
        this.comboCategoriaPopUpSubCategoria = hsom;
    }
    private HtmlInputText campoDescricaoSubCategoria = new HtmlInputText();

    public HtmlInputText getCampoDescricaoSubCategoria() {
        return campoDescricaoSubCategoria;
    }

    public void setCampoDescricaoSubCategoria(HtmlInputText hit) {
        this.campoDescricaoSubCategoria = hit;
    }
    private PopupBean panelPopup1Bean3 = new PopupBean();

    public PopupBean getPanelPopup1Bean3() {
        return panelPopup1Bean3;
    }

    public void setPanelPopup1Bean3(PopupBean pb) {
        this.panelPopup1Bean3 = pb;
    }
    private HtmlOutputText textoPopUp = new HtmlOutputText();

    public HtmlOutputText getTextoPopUp() {
        return textoPopUp;
    }

    public void setTextoPopUp(HtmlOutputText hot) {
        this.textoPopUp = hot;
    }
    private HtmlCommandButton btnPopUp1 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp1() {
        return btnPopUp1;
    }

    public void setBtnPopUp1(HtmlCommandButton hcb) {
        this.btnPopUp1 = hcb;
    }
    private HtmlCommandButton btnPopUp2 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp2() {
        return btnPopUp2;
    }

    public void setBtnPopUp2(HtmlCommandButton hcb) {
        this.btnPopUp2 = hcb;
    }
    private PanelPopup painelPopUpAviso = new PanelPopup();

    public PanelPopup getPainelPopUpAviso() {
        return painelPopUpAviso;
    }

    public void setPainelPopUpAviso(PanelPopup pp) {
        this.painelPopUpAviso = pp;
    }
    private HtmlSelectOneMenu comboMarcaPopUpCategoria = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getComboMarcaPopUpCategoria() {
        return comboMarcaPopUpCategoria;
    }

    public void setComboMarcaPopUpCategoria(HtmlSelectOneMenu hsom) {
        this.comboMarcaPopUpCategoria = hsom;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public Produtos() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        this.inicializaCampos();
        painelPopUpAviso.setRendered(false);
        

        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
            
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Produtos Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected ApplicationBean1 getview$ApplicationBean1() {
        return (ApplicationBean1) getBean("view$ApplicationBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected RequestBean1 getview$RequestBean1() {
        return (RequestBean1) getBean("view$RequestBean1");
    }

    /**
     * <p>Retornar uma referência para o Bean de dados limitados.</p>
     *
     * @retorna uma referência para o bean de dados limitados
     */
    protected SessionBean1 getview$SessionBean1() {
        return (SessionBean1) getBean("view$SessionBean1");
    }
    //metodo que Inicializa os campos
    public void inicializaCampos(){
        //mostra o valor do id correto para inserção no banco
        campoCodProduto.setValue(facadeBusca.totalDeRegistros(ProdutoVO.class) + 1);
        campoCodProduto.setReadonly(true);
        this.rendPopUp();
        
       
    }

    //metodo limparCampos
    public void limparCampos() {
        comboMarca.resetValue();
        comboCategoria.resetValue();
        comboSubCategoria.resetValue();
        campoDescricao.resetValue();
        campoUnidade.resetValue();
        campoQuantidadeEstoque.resetValue();
        campoQuantidadeMinima.resetValue();
        campoPrecoCusto.resetValue();
        campoPrecoVenda.resetValue();
        campoPercentualVendas.setValue(0.0);
        
    }
    //metodo limparCampos
    public void limparCamposEntradaTxt() {
        campoUnidade.resetValue();
        campoQuantidadeEstoque.resetValue();
        campoQuantidadeMinima.resetValue();
        campoPrecoCusto.resetValue();
        campoPrecoVenda.resetValue();
        campoPercentualVendas.setValue(0.0);

    }

   //metodo para preenchimento dos valores dos combobox
    public void inicializaCombos() {
        this.setBotaoComboMarca(new DecoradorInicializaCombos().inicializarCampoMarca());
        this.setBotaoComboMarcaPopUpCategoria(new DecoradorInicializaCombos().inicializarCampoMarca());
        
        this.setBotaoComboMarcaPopUpSubCategoria(new DecoradorInicializaCombos().inicializarCampoMarca());
        
    }

    //MetodoCombos
    public void rendPopUp(){
         //Inicializa Combos - Marca/Categoria/SubCategoria
        this.inicializaCombos();
        botaoComboCategoria.clear();
        botaoComboSubCategoria.clear();
        comboCategoria.setDisabled(true);
        comboSubCategoria.setDisabled(true);

        //Inicializa PopUp´s
        painelPopUpMarca.setRendered(false);
        painelPopUpCategoria.setRendered(false);
        painelPopUpSubCategoria.setRendered(false);
       
    }

   

    //Botão CadastrarNovaMarca
    public String btn_cadastrarMarca_action() {
        try{
            if(cadastrar.cadastrarMarca(Integer.parseInt(campoCodigoMarca.getValue().toString()), campoDescricaoMarca.getValue().toString())){
            rendPopUp();
            controlaPopup(true, "Marca Cadastrada com Sucesso!", false, true, "", "Ok");
            }
        }
        catch(Exception e) {
            controlaPopup(true, e.getMessage(), false, true, "", "Ok");
        }
        return null;
    }

    //Cancela PopUpMarca
    public String linkCancelarPopUpMarca_action() {
       painelPopUpMarca.setRendered(false);       
       return null;
    }
    
    public String btn_cadastrarCatgoria_action() {
        if(comboMarcaPopUpCategoria.getValue().toString().equals("Nova Marca")||(Integer.parseInt(comboMarcaPopUpCategoria.getValue().toString())==0 )){
            controlaPopup(true, "Verifique a Marca no Cadastro de nova Categoria", false, true, "", "Ok");
            painelPopUpCategoria.setRendered(false);
        }
        else{
            try{
            if(cadastrar.cadastrarCategoria(Integer.parseInt(campoCodigoCatgoria.getValue().toString()),Integer.parseInt(comboMarcaPopUpCategoria.getValue().toString()),campoDescricaoCatgoria.getValue().toString(),0.0)){
            rendPopUp();
            controlaPopup(true, "Categoria Cadastrada com Sucesso!", false, true, "", "Ok");
            }
        }
        catch(Exception e) {
            controlaPopup(true, e.getMessage(), false, true, "", "Ok");
        }
        }
        return null;

    }

    public String btn_cadastrarSubCatgoria_action() {
        if(comboCategoriaPopUpSubCategoria.getValue().toString().equals("Nova Categoria")||(Integer.parseInt(comboCategoriaPopUpSubCategoria.getValue().toString())==0)){
            controlaPopup(true, "Verifique a Categoria no Cadastro de nova SubCategoria", false, true, "", "Ok");
            painelPopUpSubCategoria.setRendered(false);
        }

        else{
            try{
            if(cadastrar.cadastrarSubcategoria(Integer.parseInt(campoCodigoSubCategoria.getValue().toString()),Integer.parseInt(comboCategoriaPopUpSubCategoria.getValue().toString()),campoDescricaoSubCategoria.getValue().toString())){
                rendPopUp();
                controlaPopup(true, "SubCategoria Cadastrada com Sucesso!", false, true, "", "Ok");
            }
            }
            catch(Exception e) {
                controlaPopup(true, e.getMessage(), false, true, "", "Ok");
            }
        }
            
        
        return null;
    }
   
    //Cancela PopUpCategoria
    public String linkCancelarPopUpCategoria_action() {
        painelPopUpCategoria.setRendered(false);
        return null;
    }

    //ProcessValueChange - ComboMaca
    public void comboMarca_processValueChange(ValueChangeEvent vce) {
        if(comboMarca.getValue().toString().equals("Nova Marca")){
            campoCodigoMarca.setValue(facadeBusca.totalDeRegistros(MarcaVO.class) + 1);
            painelPopUpMarca.setRendered(true);
            
        }
        else{
            botaoComboCategoria.clear();
            
            this.setBotaoComboCategoria(new DecoradorInicializaCombos().montaComboCategoria(Integer.parseInt(comboMarca.getValue().toString())));
            this.setBotaoComboCategoriaPopUpSubCategoria(new DecoradorInicializaCombos().montaComboCategoria(Integer.parseInt(comboMarca.getValue().toString())));
            

            comboCategoria.setDisabled(false);
            
        }
     }

    //ProcessValueChange - ComboCategoria
    public void comboCategoria_processValueChange(ValueChangeEvent vce) {
    if(comboCategoria.getValue().toString().equals("Nova Categoria")){
         campoCodigoCatgoria.setValue(facadeBusca.totalDeRegistros(CategoriaVO.class) + 1);
         painelPopUpCategoria.setRendered(true);
    }
    else {
         botaoComboSubCategoria.clear();
         
         this.setBotaoComboSubCategoria(new DecoradorInicializaCombos().montaComboSubCategoriaComCriterio(Integer.parseInt(comboCategoria.getValue().toString())));
         comboSubCategoria.setDisabled(false);
            
      }
      
    }

    //ProcessValueChange - ComboSubCategoria
    public void comboSubCategoria_processValueChange(ValueChangeEvent vce) {
     if(comboSubCategoria.getValue().toString().equals("Nova SubCategoria")){
            campoCodigoSubCategoria.setValue(facadeBusca.totalDeRegistros(SubcategoriaVO.class) + 1);
            painelPopUpSubCategoria.setRendered(true);
      }
      
    }

    

    public String linkCancelarPopUpSubCategoria_action() {
        painelPopUpSubCategoria.setRendered(false);
        return null;
    }

    //Action CadastrarProduto
    public String btn_cadastrar_action() {
       if(!valida.verificaCampoVazio(campoDescricao)){
       if(verificaValoresCombos(comboMarca, comboCategoria, comboSubCategoria)/*&& !valida.verificaCampoVazio(campoDescricao)*/){
       aux=2;
       this.controlaPopup(true, "Confirmar Cadastro?", true, true, "Sim", "Não");
       }
        else
            this.controlaPopup (true,"Verifique a Marca/Categoria/SubCategoria no seu cadastro de Produto",false,true,"","OK");
       return null;
    }else{
           this.controlaPopup(true, "Campo obrigatório \"descrição\" em branco!", false, true, "", "OK");
    }
    return null;
    }

    public boolean verificaValoresCombos(HtmlSelectOneMenu som1,HtmlSelectOneMenu som2,HtmlSelectOneMenu som3){

        if(som1.getValue().toString().equals("Nova Marca") || Integer.parseInt(som1.getValue().toString())==0 ||
                som2.getValue().toString().equals("Nova Categoria") || Integer.parseInt(som2.getValue().toString())==0 ||
                som3.getValue().toString().equals("Nova SubCategoria") || Integer.parseInt(som3.getValue().toString())==0){

            return false;
        }

        else
            return true;

    }

    //Método ControlaPopUp
    public void controlaPopup(boolean renderedPopup, String textoPoup, boolean btn1Rendered, boolean btn2Rendered,
           String textobtn1, String textobtn2) {

      painelPopUpAviso.setRendered(renderedPopup);
      textoPopUp.setValue(textoPoup);
      btnPopUp1.setValue(textobtn1);
      btnPopUp1.setRendered(btn1Rendered);
      btnPopUp2.setRendered(btn2Rendered);
      btnPopUp2.setValue(textobtn2);
   }

    //Action Sair
    public String btn_voltar_action() {
        aux = 1;
        this.controlaPopup(true, "Deseja Sair?", true, true, "Sim", "Não");
        return null;
    }

    //Botão1 - PopUpAviso
    public String btnPopUp1_action() {
        if(aux==1){
            limparCampos();
            return "voltar";
        }
        else{
            try {
            if (cadastrar.cadastrarProduto(Integer.parseInt(comboSubCategoria.getValue().toString()),
                    Integer.parseInt(comboCategoria.getValue().toString()), Integer.parseInt(comboMarca.getValue().toString()),
                    Integer.parseInt(campoCodProduto.getValue().toString()), campoDescricao.getValue().toString(),
                    campoUnidade.getValue().toString(), Double.parseDouble(campoQuantidadeEstoque.getValue().toString()),
                    Double.parseDouble(campoQuantidadeMinima.getValue().toString()),
                    Double.parseDouble(campoPrecoCusto.getValue().toString()),
                    Double.parseDouble(campoPrecoVenda.getValue().toString()),
                    Double.parseDouble(campoPercentualVendas.getValue().toString()))) {
                this.controlaPopup (true,"Produto cadastrado com sucesso!",false,true,"","OK");
                limparCamposEntradaTxt();
            }
        } catch (Exception e) {
            controlaPopup(true,"Erro:" + e.getMessage() + "caused by: " + e.getStackTrace(),false,true,"","OK");
            System.err.println("Erro:" + e.getMessage() + "caused by: " + e.getStackTrace());
            e.getStackTrace();
            }
        }//fim do else
        
           
        return null;
    }

    //Botão2 - PopUpAviso
    public String btnPopUp2_action() {        
        painelPopUpAviso.setRendered(false);
        return null;
    }

    public void campoPercentualVendas_processValueChange(ValueChangeEvent vce) {
        if(campoPercentualVendas.getValue().toString().equals("") || campoPercentualVendas.getValue().toString().equals(0.0)){
            campoPercentualVendas.setValue(0.0);
            double precoV = Double.parseDouble(campoPrecoCusto.getValue().toString())+(Double.parseDouble(campoPrecoCusto.getValue().toString())*(Double.parseDouble(campoPercentualVendas.getValue().toString())/100));
            campoPrecoVenda.setValue(precoV);
        }
        else if(!campoPrecoCusto.getValue().toString().equals("")){
            double precoV = Double.parseDouble(campoPrecoCusto.getValue().toString())*(1+(Double.parseDouble(campoPercentualVendas.getValue().toString())/100));
            campoPrecoVenda.setValue(precoV);
            }
            else
                campoPrecoCusto.setValue(0.0);
    }

    public void campoPrecoCusto_processValueChange(ValueChangeEvent vce) {
        if(campoPrecoCusto.getValue().toString().equals("") || campoPrecoCusto.getValue().toString().equals(0.0)){
            campoPrecoCusto.setValue(0.0);
        }
        else{
            Double precoCusto = Double.parseDouble(campoPrecoCusto.getValue().toString());
            Double percVenda = Double.parseDouble(campoPercentualVendas.getValue().toString());
            campoPrecoVenda.setValue(precoCusto*(1+percVenda));
        }
    }

}

  

